package com.wes.dsptest.audioalarm;

import java.text.SimpleDateFormat;
import java.util.Date;

public class alarmTimeUtil {

    public static long getTimeDifference(String startTimeStr, String endTimeStr) {
        long timeDiff = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            Date startDate = format.parse(startTimeStr);
            Date endDate = format.parse(endTimeStr);

            if(endDate.getTime() > startDate.getTime())
            {
                startDate.setTime(startDate.getTime() + 24*60*60*1000);
            }

            timeDiff = endDate.getTime() - startDate.getTime(); // 获取两个时间点的时间差，单位为毫秒
            if(timeDiff < 0)
                timeDiff = startDate.getTime() - endDate.getTime(); // 获取两个时间点的时间差，单位为毫秒
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeDiff;
    }

    public static String getStringTimeDifference(String startTimeStr, String endTimeStr) {
        return getFormattedTime(getTimeDifference(startTimeStr, endTimeStr));
    }

    public static String getFormattedTime(long timeDiff) {
        int hours = (int) (timeDiff / (1000 * 60 * 60)); // 将毫秒转换成小时
        int minutes = (int) ((timeDiff - hours * (1000 * 60 * 60)) / (1000 * 60)); // 将毫秒转换成分钟
        int seconds = (int) ((timeDiff - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000); // 将毫秒转换成秒

        String formattedTime = String.format("%02d:%02d:%02d", hours, minutes, seconds);
        return formattedTime;
    }
}

